home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1996 April / MacFormat CD Edition MF36 (April 1996).iso / Shareware City / Developers / Tools Plus - GUI⁄Event libs / Tools Plus 2.6.1a Evaluat'n Kit / Tools Plus 2.6.1a / Tutorials / 4-Editing Fields / Tutorial.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-08-01  |  6.6 KB  |  231 lines  |  [TEXT/KAHL]

  1. //    Tools Plus Tutorial -- Editing Fields
  2.  
  3.  
  4.  
  5.  
  6.  #include "ToolsPlus.h"
  7.  #include "String.h"                        // ANSI C string manipulation
  8.  #include "PascalStrHandles.c"  // Quick 'n' Dirty routines for copying a C string to a
  9.                                                                  //        Pascal "String Handle" and vice versa.
  10.  
  11.  
  12.  #define ApplMenu        0                        // Menu constants to make code more readable…
  13.  #define FileMenu        1
  14.  #define EditMenu        2
  15.  
  16.  #define bToField        1                        // "To Field" and "From Field" buttons…
  17.  #define bFromField    2
  18.  
  19.  
  20.  
  21.  typedef        unsigned char            Str30[31];    // A 30-character string
  22.  
  23.  
  24.  
  25.  TPPollRecord        Poll;                                // Polling record to retrieve event information
  26.  Boolean                ExitTheDemo;                // Should the demo terminate?
  27.  
  28.  Handle                    hField1;                        // Handle to 1st editing field's 30-char string
  29.  Handle                    hField2, hField3;        // Handles to 2nd and 3rd editing fields' 255-char string
  30.  short                    Field;                            // Field number
  31.  
  32.  Str255                    EditString;                    // Field's text
  33.  short                    theButton;                    // Button number clicked in an alert
  34.  
  35.  
  36.  
  37.  void ApplicationInitialization (void);
  38.  void ProcessMenuSelection (void);
  39.  
  40.  
  41.  
  42.  
  43.  
  44.  
  45.  //    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -
  46.  void main(void)
  47.  {
  48.     InitGraf(&qd.thePort);
  49.     InitFonts();
  50.     InitWindows();
  51.     InitMenus();
  52.     TEInit();
  53.     InitDialogs(0L);
  54.     MaxApplZone();
  55.  
  56.  
  57.     if (!InitToolsPlus(1, 1, UseColor))
  58.         ExitToShell();
  59.  
  60.     ApplicationInitialization();
  61.  
  62.     while (!ExitTheDemo)                    //Main Event Loop
  63.         {
  64.         if (PollSystem(&Poll))            //If an event is available, process the event…
  65.             {
  66.             switch (Poll.What)
  67.                 {
  68.                 case doMenu:
  69.                     ProcessMenuSelection();
  70.                     break;
  71.  
  72.  
  73.                 case doGoAway:            // User clicked a window's close box
  74.                     WindowClose(Poll.Window);
  75.                     break;
  76.  
  77.  
  78.                 case doKeyDown: case doAutoKey:    // Typing (check for Tab and Shift/Tab)…
  79.                     // If key-stroke is in window 1, it's a tab, and the Command, Option and
  80.                     //        Control keys were not down…
  81.                     if ((Poll.Key.Chr == TabKey) && !(Poll.Modifiers.Bits.CmdKey || Poll.Modifiers.Bits.OptionKey || Poll.Modifiers.Bits.ControlKey))
  82.                         {
  83.                         SaveFieldString();                                            // Save the field's edited text as the field's string
  84.                         Field = ActiveFieldNumber();                        // Determine the active field number
  85.                         if (!Poll.Modifiers.Bits.ShiftKey)            // TAB: to next field…
  86.                             Field = Field + 1 - 3 * (Field == 3);    //        Add 1.  If field=3, start at 1 again.
  87.                         else                                                                        // SHIFT-TAB: to previous field…
  88.                             Field = Field - 1 + 3 * (Field == 1);    //        Subtract 1.  If field=1, start at 3.
  89.                         ActivateField(Field, teSelectAll);            // Select all the text in the newly activated field
  90.                         }
  91.                     break;
  92.  
  93.  
  94.                 case doClickField:         // User clicked in an inactive field…
  95.                     CurrentWindow(1);        // The following commands apply to window #1…
  96.                     SaveFieldString();    // Save the field's edited text as the field's string
  97.                     ClickInField();            // Process the click in the inactive field
  98.                     break;
  99.  
  100.  
  101.  
  102.                 case doButton:                // User clicked a button…
  103.                     switch (Poll.Button.Num)
  104.                         {
  105.                         case bToField:        // Write into field #3…
  106.                             PasteIntoField(3, "\pThis is the replacement text pasted by the application", teReplace);
  107.                             break;
  108.  
  109.                         case bFromField:    // Display contents of field #3…
  110.                             if (ActiveFieldNumber() == 3)        // If user is editing field #3…
  111.                                 GetFieldString(EditString);        //        get the EDITED text.
  112.                             else
  113.                                 BlockMove(*hField3, EditString, **hField3);    // Copy field's (permanent) text to EditString
  114.  
  115.                             theButton = AlertBox(noteIcon, EditString, -OkAlert);
  116.                             break;
  117.                         }
  118.                     break;
  119.  
  120.  
  121.                 default:
  122.                     break;                            //All other events are ignored
  123.                 }
  124.             }
  125.         }
  126.     }
  127.  
  128.  
  129.  //    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -
  130.  void ApplicationInitialization (void)
  131.     {
  132.     //    Do all the application setup before you start polling for events…
  133.  
  134.      #define        DemoWindow1        1
  135.  
  136.     // Create an Apple menu with an 'About…' item…
  137.     AppleMenu("\pAbout My App…");
  138.  
  139.     // Create the standard File and Edit menus…
  140.     Menu(FileMenu, 0, enabled, "\pFile");
  141.     Menu(FileMenu, 1, disabled, "\pNew/N");
  142.     Menu(FileMenu, 2, disabled, "\pOpen/O");
  143.     Menu(FileMenu, 3, disabled, "\pClose/W");
  144.     Menu(FileMenu, 4, disabled, mDividingLine);
  145.     Menu(FileMenu, 5, enabled, "\pQuit/Q");
  146.  
  147.     Menu(EditMenu, 0, enabled, "\pEdit");
  148.     Menu(EditMenu, 1, disabled, "\pUndo/Z");
  149.     Menu(EditMenu, 2, disabled, mDividingLine);
  150.     Menu(EditMenu, 3, disabled, "\pCut/X");
  151.     Menu(EditMenu, 4, disabled, "\pCopy/C");
  152.     Menu(EditMenu, 5, disabled, "\pPaste/V");
  153.     Menu(EditMenu, 6, disabled, "\pClear");
  154.     Menu(EditMenu, 7, disabled, mDividingLine);
  155.     Menu(EditMenu, 8, disabled, "\pShow Clipboard…");
  156.     UpdateMenuBar();
  157.  
  158.  
  159.     WindowOpen(DemoWindow1, 5, 40, 206, 312, "\pEditing Field Tutorial", noGrowDocProc, GoAway, NotModal);
  160.  
  161.     // Allocate text handles for editing fields, and initialize to a null string…
  162.     hField1 = (Handle)NewStrHandle(30);
  163.     hField2 = (Handle)NewStrHandle(255);
  164.     hField3 = (Handle)NewStrHandle(255);
  165.  
  166.     // Set each field's text to a default value…
  167.     Cstr2PHdl("Length limited field", hField1);
  168.     Cstr2PHdl("Single line editing field.", hField2);
  169.     Cstr2PHdl("This is a multiple-line editing field which incorporates word wrap.", hField3);
  170.  
  171.     // Create first field using Monaco 9pt. It is length limited to 30 characters…
  172.     TextFont(monaco);
  173.     TextSize(9);
  174.     FieldLengthLimit(on);
  175.     NewField(1, 10, 10, 191, 21, hField1, teBoxNoCR, teJustLeft);
  176.     FieldLengthLimit(off);
  177.  
  178.     // Create second field using Geneva 9pt bold. This is a single-line field…
  179.     TextFont(geneva);
  180.     TextFace(bold);
  181.     NewField(2, 10, 30, 191, 42, hField2, teBoxNoCR, teJustLeft);
  182.  
  183.     // Create third field using Chicago 12pt.    This is a multiple line editing field…
  184.     TextFont(systemFont);
  185.     TextSize(12);
  186.     TextFace(0);
  187.     NewField(3, 10, 51, 191, 227, hField3, teBoxCR, teJustLeft);
  188.  
  189.     ActivateField(1, teSelectEnd);    // Activate the first field with insertion point at the end of the text
  190.  
  191.  
  192.     NewButton(bToField, 6, 238, 95, 258, "\pWrite to #3", pushButProc, enabled, notSelected);
  193.     NewButton(bFromField, 105, 238, 195, 258, "\pGet from #3", pushButProc, enabled, notSelected);
  194.     ExitTheDemo = false;
  195.     }
  196.  
  197.  
  198.  
  199.  //    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -
  200.  void ProcessMenuSelection (void)
  201.     {
  202.      #define        NewItem        1
  203.      #define        OpenItem    2
  204.      #define        CloseItem    3
  205.      #define        QuitItem    5
  206.      
  207.      short            theButton;
  208.  
  209.     switch (Poll.Menu.Num)
  210.         {
  211.         case ApplMenu:    // Apple menu's 'About…' item
  212.             theButton = AlertBox(NoIcon, "\pThis is my application’s about box.  (Click to close)", NoButtonAlert);
  213.             break;
  214.             
  215.         case FileMenu:
  216.             switch (Poll.Menu.Item)
  217.                 {
  218.                 case NewItem:
  219.                     break;
  220.                 case OpenItem:
  221.                     break;
  222.                 case CloseItem:
  223.                     break;
  224.                 case QuitItem:
  225.                     ExitTheDemo = true;
  226.                     break;
  227.                 }
  228.         }
  229.  
  230.     MenuHilite(0);    // Turn off highlighted menu
  231.     }